
use "$base_out/learning_summary_2015.dta", clear
decode cct_num, gen(cct_num2)
drop cct_num
rename cct_num2 cct_num
merge m:1 cct_num using "$base_out/management_summary_2015.dta"
drop _merge
rename cct_num school_id
merge m:1 school_id using "$base_out/DWMS_Regresiones", keepus(enumerador urbana- promovidos Marg)

merge m:1 school_id using "$basein/Datos adicionales/secundarias.dta", gen(_secundaria)
keep if _secundaria==3
keep if secondary!=1

rename school_id cct

label var management_sted1 "DWMS"
*label var leader_sted1 "Leadership"
label var operations_sted1 "Operations"
label var monitoring_sted1 "Monitoring"
label var targets_sted1 "Targets"
label var people_sted1 "People"

*** Control****
eststo clear
eststo: reg prom_pca management_sted1, vce(cluster cct)
matrix tempm=e(b)
local tempm=string(tempm[1,1], "%9.2fc")
file open newfile using "$tables/DWMS_2015_1.tex", write replace
file write newfile "`tempm'"
file close newfile
test management_sted1
if r(p)<0.001 {
	di "peque"
	local tempm ="$<0.001$"
	file open newfile using "$tables/DWMS_2015_1_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
if r(p)>0.001 {
	di "grande"
	local tempm=string(r(p), "%9.2gc")
	file open newfile using "$tables/DWMS_2015_1_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
		
		
eststo: reghdfe prom_pca management_sted1 , abs(state) vce(cluster cct)
matrix tempm=e(b)
local tempm=string(tempm[1,1], "%9.2fc")
file open newfile using "$tables/DWMS_2015_2.tex", write replace
file write newfile "`tempm'"
file close newfile
test management_sted1
if r(p)<0.001 {
	di "peque"
	local tempm ="$<0.001$"
	file open newfile using "$tables/DWMS_2015_2_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
if r(p)>0.001 {
	di "grande"
	local tempm=string(r(p), "%9.2gc")
	file open newfile using "$tables/DWMS_2015_2_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}


eststo: reghdfe prom_pca management_sted1, abs(estrato) vce(cluster cct)
matrix tempm=e(b)
local tempm=string(tempm[1,1], "%9.2fc")
file open newfile using "$tables/DWMS_2015_3.tex", write replace
file write newfile "`tempm'"
file close newfile
test management_sted1
if r(p)<0.001 {
	di "peque"
	local tempm ="$<0.001$"
	file open newfile using "$tables/DWMS_2015_3_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
if r(p)>0.001 {
	di "grande"
	local tempm=string(r(p), "%9.2gc")
	file open newfile using "$tables/DWMS_2015_3_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}


eststo: reghdfe prom_pca management_sted1 urbana Marg str  insc_tot , abs(estrato) vce(cluster cct)
matrix tempm=e(b)
local tempm=string(tempm[1,1], "%9.2fc")
file open newfile using "$tables/DWMS_2015_4.tex", write replace
file write newfile "`tempm'"
file close newfile
test management_sted1
if r(p)<0.001 {
	di "peque"
	local tempm ="$<0.001$"
	file open newfile using "$tables/DWMS_2015_4_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
if r(p)>0.001 {
	di "grande"
	local tempm=string(r(p), "%9.2gc")
	file open newfile using "$tables/DWMS_2015_4_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}

eststo: reghdfe prom_pca management_sted1 urbana Marg str  insc_tot, abs(estrato enumerador) vce(cluster cct)
matrix tempm=e(b)
local tempm=string(tempm[1,1], "%9.2fc")
file open newfile using "$tables/DWMS_2015_5.tex", write replace
file write newfile "`tempm'"
file close newfile
test management_sted1
if r(p)<0.001 {
	di "peque"
	local tempm ="$<0.001$"
	file open newfile using "$tables/DWMS_2015_5_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
if r(p)>0.001 {
	di "grande"
	local tempm=string(r(p), "%9.2gc")
	file open newfile using "$tables/DWMS_2015_5_pvalue.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}

esttab using "$tables/DWMS_2015.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(management_sted1 ) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)

	

eststo clear
*eststo: reghdfe prom_pca leader_sted1  urbana Marg str  insc_tot , abs(estrato enumerador) vce(cluster cct)
eststo: reghdfe prom_pca operations_sted1  urbana Marg str  insc_tot , abs(estrato enumerador) vce(cluster cct)
eststo: reghdfe prom_pca monitoring_sted1  urbana Marg str  insc_tot , abs(estrato enumerador) vce(cluster cct)
eststo: reghdfe prom_pca targets_sted1  urbana Marg str  insc_tot , abs(estrato enumerador) vce(cluster cct)
eststo: reghdfe prom_pca people_sted1 urbana Marg str  insc_tot, abs(estrato enumerador) vce(cluster cct)


esttab using "$tables/DWMS_2015_Detailed.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(operations_sted1 monitoring_sted1 targets_sted1 people_sted1) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
	
	
reghdfe prom_pca  urbana Marg str  insc_tot , abs(estrato enumerador) residuals(resid_prom_pca) vce(cluster cct)
reghdfe management_sted1  urbana Marg str  insc_tot , abs(estrato enumerador) residuals(resid_index_sted1) vce(cluster cct)

 
preserve
collapse (mean) management_sted1 prom_pca urbana Marg str insc_tot resid_prom_pca resid_index_sted1 (count) peso=prom_pca, by( treatgroup2 estrato state cct)   //by( treatgroup2 estrato state cct)
twoway (scatter prom_pca management_sted1 [aw=peso], msymbol(Oh) sort graphregion(color(white))  ylab(,labsize(large) nogrid) xlab(,labsize(large)) xtitle("Management score",size(large)) ytitle("Test scores",size(large))  leg(off))  (lfit prom_pca management_sted1 [aw=peso],  leg(off)) 
graph export "$graphs/ManagementLearning_2015.pdf", replace
twoway (scatter resid_prom_pca resid_index_sted1  [aw=peso], msymbol(Oh) sort graphregion(color(white))  ylab(,labsize(large) nogrid) xlab(,labsize(large)) xtitle("Residual management score",size(large)) ytitle("Residual test scores",size(large))  leg(off))  (lfit resid_prom_pca resid_index_sted1 [aw=peso],  leg(off)) 
graph export "$graphs/ManagementLearning_Residuals_2015.pdf", replace

twoway (scatter prom_pca management_sted1, mlabel(state) msymbol(O) sort graphregion(color(white))  ylab(,labsize(large) nogrid) xlab(,labsize(large)) xtitle("Management score",size(large)) ytitle("Test scores",size(large))  leg(off))  (lfit prom_pca management_sted1,  leg(off)) 
graph export "$graphs/ManagementLearning_2015_byState.pdf", replace
restore

